package com.genreate.util.xinyuan;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.genreate.util.linuxUtil.LinuxUtil;
import com.genreate.util.xinyuan.constant.XyConstant;
import com.jcraft.jsch.Session;

import java.util.Date;

/**
 * 海外售后系统 打包部署到linux
 * 国内正式和测试
 *
 * @author LYS
 * @date 2025-01-06
 */
public class HaiwaiChinaDevProd {
    public static final String serverIp = "192.168.8.74" ;
    public static final int port = 22;
    public static final String username = "root" ;
    public static final String password = "Oversea.2024" ;

    public static final Session session = LinuxUtil.createSession(serverIp, port, username, password);

    /**
     * 项目路径前缀 不带//后缀
     */
    public static final String projectJarPath = "D:\\ideaProjects\\xyChinaProdDev\\overseas" ;

    /**
     * 项目路径前缀  带//后缀
     */
    public static final String projectJarPathPrefix = projectJarPath + "\\" ;

    /**
     * linux jar路径前缀
     */
    public static final String linuxJarPathPrefix = "/root/project/" ;

    /**
     * linux view 路径前缀
     */
    public static final String linuxViewPathPrefix = "/var/lib/docker/volumes/project_nginx-html/_data" ;

    public static void main(String[] args) {
        all();
    }

    public static void all() {
        //测试服

        //manage 后端
//        backManageChinaDev();
//        manageChinadev();
//        manageChinadevPath();

        //customer 后端
//        customerChinadev();
//        customerChinadevPath();

        //camunda 后端
//        camundaChinadev();

        //manage 前端
//        backViewChinaDevManage();
//        viewChinadevManage();

        //customer 前端
//        viewChinadevCustomer();





        //国内正式服

        //manage 后端
        backManageChinaProd();
        manageChinaProd();
        //manage 前端
//        backViewChinaProdManage();
//        viewChinaProdManage();


        //customer 后端
        backCustomerChinaProd();
        customerChinaProd();

        //customer 前端
//        backViewChinaProdCustomer();
//        viewChinaProdCustomer();

        //camunda 后端
//        backCamundaChinaProd();
//        camundaChinaProd();
        session.disconnect();
    }

    /**
     * 备份国内测试服环境的 前端文件 manage
     */
    public static void backViewChinaDevManage() {
        //要复制的文件夹路径 前缀
        String fileFloderPathPrefix = "/var/lib/docker/volumes/project_nginx-html/_data" ;
        //要复制的文件夹名称
        String fileFloderName = "manage-test" ;
        //要存放备份文件的路径
        String backFloderPath = "/var/lib/docker/volumes/project_nginx-html/_data/manage-test-bak" ;
        //压缩包的名字
        String zipFileName = StrUtil.format("{}manage-test.zip", DateUtil.format(new Date(), DatePattern.PURE_DATETIME_PATTERN));
        LinuxUtil.createZipMv(session, fileFloderPathPrefix, fileFloderName, backFloderPath, zipFileName);
    }

    /**
     * 备份国内正式环境的 前端文件 manage
     */
    public static void backViewChinaProdManage() {
        //要复制的文件夹路径 前缀
        String fileFloderPathPrefix = "/var/lib/docker/volumes/project_nginx-html/_data" ;
        //要复制的文件夹名称
        String fileFloderName = "manage-prd" ;
        //要存放备份文件的路径
        String backFloderPath = "/var/lib/docker/volumes/project_nginx-html/_data/manage-prd-back" ;
        //压缩包的名字
        String zipFileName = StrUtil.format("{}manage-prd.zip", DateUtil.format(new Date(),DatePattern.PURE_DATETIME_PATTERN));
        LinuxUtil.createZipMv(session, fileFloderPathPrefix, fileFloderName, backFloderPath, zipFileName);
    }

    /**
     * 备份国内正式环境的 前端文件 customer
     */
    public static void backViewChinaProdCustomer() {
        //要复制的文件夹路径 前缀
        String fileFloderPathPrefix = "/var/lib/docker/volumes/project_nginx-html/_data" ;
        //要复制的文件夹名称
        String fileFloderName = "customer-prd" ;
        //要存放备份文件的路径
        String backFloderPath = "/var/lib/docker/volumes/project_nginx-html/_data/customer-prd-back" ;
        //压缩包的名字
        String zipFileName = StrUtil.format("{}customer-prd.zip", DateUtil.format(new Date(),DatePattern.PURE_DATETIME_PATTERN));
        LinuxUtil.createZipMv(session, fileFloderPathPrefix, fileFloderName, backFloderPath, zipFileName);
    }

    /**
     * 备份国内测式环境的 后端jar文件
     */
    public static void backManageChinaDev() {
        String cpCommand = StrUtil.format("cp" +
                " /root/project/overseas-shop-manage-dev/package/app-manage.jar" +
                " /root/project/overseas-shop-manage-dev/bak/{}app-manage.jar", DateUtil.format(new Date(),DatePattern.PURE_DATETIME_PATTERN));
        LinuxUtil.executeCommand(session, cpCommand);
    }

    /**
     * 备份国内测式环境的 后端jar文件 manage
     */
    public static void backManageChinaProd() {
        String cpCommand = StrUtil.format("cp" +
                " /root/project/overseas-shop-manage-prd/package/app-manage.jar" +
                " /root/project/overseas-shop-manage-prd/bak/{}app-manage.jar", DateUtil.format(new Date(),DatePattern.PURE_DATETIME_PATTERN));
        LinuxUtil.executeCommand(session, cpCommand);
    }

    /**
     * 备份国内测式环境的 后端jar文件 camunda
     */
    public static void backCamundaChinaProd() {
        String cpCommand = StrUtil.format("cp" +
                " /root/project/overseas-shop-camunda-prd/package/app-camunda.jar" +
                " /root/project/overseas-shop-camunda-prd/bak/{}app-camunda.jar", DateUtil.format(new Date(),DatePattern.PURE_DATETIME_PATTERN));
        LinuxUtil.executeCommand(session, cpCommand);
    }

    /**
     * 备份国内测式环境的 后端jar文件 customer
     */
    public static void backCustomerChinaProd() {
        String cpCommand = StrUtil.format("cp" +
                " /root/project/overseas-shop-customer-prd/package/app-customer.jar" +
                " /root/project/overseas-shop-customer-prd/bak/{}app-customer.jar", DateUtil.format(new Date(),DatePattern.PURE_DATETIME_PATTERN));
        LinuxUtil.executeCommand(session, cpCommand);
    }

    /**
     * 前端模块 国内 测试 manage
     */
    private static void viewChinadevManage() {
        String localFilePath =  XyConstant.MANGE_DEV_VIEW_PATH;
        String remoteFilePath = linuxViewPathPrefix + "/manage-test.zip" ;
        String extractCommand = "unzip -o " + remoteFilePath + " -d " + linuxViewPathPrefix + "/manage-test" ;

        LinuxUtil.copyFileExecuteCommand(session, localFilePath, remoteFilePath, extractCommand);
    }

    /**
     * 前端模块 国内 测试 manage
     */
    private static void viewChinadevManageHaveDirectory() {
        String localFilePath = "D:\\wxData\\wxMsg\\WeChat Files\\wxid_u9kughnafyqm21\\FileStorage\\File\\2025-05\\manage-test\\manage-test\\manage-test.zip";
        String remoteFilePath = linuxViewPathPrefix + "/manage-test.zip" ;
        String extractCommand = "unzip -o " + remoteFilePath + " -d " + linuxViewPathPrefix ;

        LinuxUtil.copyFileExecuteCommand(session, localFilePath, remoteFilePath, extractCommand);
    }

    /**
     * 前端模块 国内 测试 customer
     */
    private static void viewChinadevCustomer() {
        String localFilePath = XyConstant.CUSTOMER_DEV_VIEW_PATH;
        String remoteFilePath = linuxViewPathPrefix + "/customer-test.zip" ;
        String extractCommand = "unzip -o " + remoteFilePath + " -d " + linuxViewPathPrefix + "/customer-test" ;

        LinuxUtil.copyFileExecuteCommand(session, localFilePath, remoteFilePath, extractCommand);
    }

    /**
     * 前端模块 国内 正式 manage
     */
    private static void viewChinaProdManage() {
        String localFilePath = XyConstant.MANGE_PROD_VIEW_PATH;
        String remoteFilePath = linuxViewPathPrefix + "/manage-prd.zip" ;
        String extractCommand = "unzip -o " + remoteFilePath + " -d " + linuxViewPathPrefix + "/manage-prd" ;

        LinuxUtil.copyFileExecuteCommand(session, localFilePath, remoteFilePath, extractCommand);
    }

    /**
     * 前端模块 国内 正式 customer
     */
    private static void viewChinaProdCustomer() {
        String localFilePath = XyConstant.CUSTOMER_PROD_VIEW_PATH;
        String remoteFilePath = linuxViewPathPrefix + "/customer-prd.zip" ;
        String extractCommand = "unzip -o " + remoteFilePath + " -d " + linuxViewPathPrefix + "/customer-prd" ;

        LinuxUtil.copyFileExecuteCommand(session, localFilePath, remoteFilePath, extractCommand);
    }

    /**
     * 国内 后台 测试
     */
    private static void manageChinadev() {
        String localFilePath = projectJarPathPrefix + "manage\\target\\app-manage.jar" ;
        String remoteFilePath = linuxJarPathPrefix + "overseas-shop-manage-dev/package/app-manage.jar" ;
        String dockerRestartCommand = "docker restart overseas-shop-manage-dev" ;

        LinuxUtil.copyFileExecuteCommand(session, localFilePath, remoteFilePath, dockerRestartCommand);
    }

    private static void manageChinadevPath() {
        String localFilePath = "D:\\wxData\\wxMsg\\WeChat Files\\wxid_u9kughnafyqm21\\FileStorage\\File\\2025-05\\app-manage(3).jar" ;
        String remoteFilePath = linuxJarPathPrefix + "overseas-shop-manage-dev/package/app-manage.jar" ;
        String dockerRestartCommand = "docker restart overseas-shop-manage-dev" ;

        LinuxUtil.copyFileExecuteCommand(session, localFilePath, remoteFilePath, dockerRestartCommand);
    }

    /**
     * 国内 客户端 测试
     */
    private static void customerChinadev() {
        String localFilePath = projectJarPathPrefix + "customer\\target\\app-customer.jar" ;
        String remoteFilePath = linuxJarPathPrefix + "overseas-shop-customer-dev/package/app-customer.jar" ;
        String dockerRestartCommand = "docker restart overseas-shop-customer-dev" ;

        LinuxUtil.copyFileExecuteCommand(session, localFilePath, remoteFilePath, dockerRestartCommand);
    }

    /**
     * 国内 客户端 测试
     */
    private static void customerChinadevPath() {
        String localFilePath = "D:\\wxData\\wxMsg\\WeChat Files\\wxid_u9kughnafyqm21\\FileStorage\\File\\2025-05\\app-customer(2).jar" ;
        String remoteFilePath = linuxJarPathPrefix + "overseas-shop-customer-dev/package/app-customer.jar" ;
        String dockerRestartCommand = "docker restart overseas-shop-customer-dev" ;

        LinuxUtil.copyFileExecuteCommand(session, localFilePath, remoteFilePath, dockerRestartCommand);
    }

    /**
     * 国内 客户端 正式
     */
    private static void customerChinaProd() {
        String localFilePath = projectJarPathPrefix + "customer\\target\\app-customer.jar" ;
        String remoteFilePath = linuxJarPathPrefix + "overseas-shop-customer-prd/package/app-customer.jar" ;
        String dockerRestartCommand = "docker restart overseas-shop-customer-prd" ;

        LinuxUtil.copyFileExecuteCommand(session, localFilePath, remoteFilePath, dockerRestartCommand);
    }

    /**
     * 国内 camunda 测试
     */
    private static void camundaChinadev() {
        String localFilePath = projectJarPathPrefix + "camunda\\target\\app-camunda.jar" ;
        String remoteFilePath = linuxJarPathPrefix + "overseas-shop-camunda-dev/package/app-camunda.jar" ;
        String dockerRestartCommand = "docker restart overseas-shop-camunda-dev" ;

        LinuxUtil.copyFileExecuteCommand(session, localFilePath, remoteFilePath, dockerRestartCommand);
    }

    /**
     * 国内 camunda 正式
     */
    private static void camundaChinaProd() {
        String localFilePath = projectJarPathPrefix + "camunda\\target\\app-camunda.jar" ;
        String remoteFilePath = linuxJarPathPrefix + "overseas-shop-camunda-prd/package/app-camunda.jar" ;
        String dockerRestartCommand = "docker restart overseas-shop-camunda-prd" ;

        LinuxUtil.copyFileExecuteCommand(session, localFilePath, remoteFilePath, dockerRestartCommand);
    }

    /**
     * 国内 后台 正式 manage
     */
    private static void manageChinaProd() {
        String localFilePath = projectJarPathPrefix + "manage\\target\\app-manage.jar" ;
        String remoteFilePath = linuxJarPathPrefix + "overseas-shop-manage-prd/package/app-manage.jar" ;
        String dockerRestartCommand = "docker restart overseas-shop-manage-prd" ;

        LinuxUtil.copyFileExecuteCommand(session, localFilePath, remoteFilePath, dockerRestartCommand);
    }
}
